package counter.com.baomidou.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import counter.com.baomidou.entity.Trojan;
import counter.com.baomidou.service.ITrojanService;
import counter.com.common.FileProperties;
import counter.com.common.MyTool;
import counter.com.common.Result;
import counter.com.configuration.LoudongConfiguration;
import counter.com.connproperty.Trojan.TrojanAddProperty;
import counter.com.connproperty.Trojan.TrojanSearchProperty;
import counter.com.myservice.FileController;
import counter.com.myservice.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-09
 */
@RestController
@RequestMapping("/trojan")
public class TrojanController {
    @Autowired
    private FileService fileService;
    @Autowired
    FileController fileController;
    /*一条木马记录默认添加的图片地址*/
    private static final String DEFAULT_IMURL = "/file/download/centos.png";
    @Autowired
    FileProperties fileProperties;
    @Autowired
    LoudongConfiguration counterLoudongConfiguration;
    @Autowired
    ITrojanService service;
    private static final String[] nationList = {"America", "China", "Japan", "India", "Russia", "German", "France", "The UK"};
    private static final String[] osList = {"Ubuntu 14.0", "Ubuntu 16.0", "Ubuntu 18.0", "Centos 6.0", "Centos 7.0", "Windows7 x64", "Windows10 x64"};

    @PostMapping("/search")
    public Result search(@RequestBody TrojanSearchProperty data) {
        LambdaQueryWrapper<Trojan> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Trojan::getId);
        Page<Trojan> page = new Page<>(data.getPage(), data.getPagesize());
        IPage<Trojan> iPage = service.page(page, wrapper);
        return Result.success(iPage);
    }

    @PostMapping("/add")
    public Result add(@RequestBody @Validated TrojanAddProperty data) {
        Trojan trojan = new Trojan();
        LambdaQueryWrapper<Trojan> wrapper = new LambdaQueryWrapper<>();
        //将上传文件的名称 修改为实际的名称
        if (modifyFileName(data.getUploadfilename(), data.getExefilename())) {
            trojan.setImurl(DEFAULT_IMURL);
            trojan.setDstip(data.getDstip());
            trojan.setDstport(data.getDstport());
            trojan.setExefilepath(fileProperties.getDownloadUrlPrefix() + "/" + data.getExefilename());
            trojan.setExefilename(data.getExefilename());
            trojan.setDstusername(data.getTargetname());
            trojan.setTargetname(data.getTargetname());
            trojan.setCreatedtime(LocalDateTime.now());
            trojan.setNation(generateNation());
            trojan.setName(data.getTargetname() + "-PC");
            trojan.setOs(generateOs());
            System.out.println(data);
            if (service.save(trojan)) {
                return Result.success("成功添加");
            } else {
                return Result.fail("添加失败");
            }
        } else {
            return Result.fail("上传文件遗失");
        }
    }

    private boolean modifyFileName(String oldName, String newName) {
        Path fileStorageLocation = Paths.get(fileProperties.getUpLoadDir()).toAbsolutePath().normalize();
        System.out.println("DEBUG");
        System.out.println(fileStorageLocation);
        File file = new File(fileStorageLocation + "\\" + oldName);
        return file.renameTo(new File(fileStorageLocation + "\\" + newName));
    }

    private String generateOs() {
        return osList[MyTool.getRandomInt(0, osList.length)];
    }

    private String generateNation() {
        return nationList[MyTool.getRandomInt(0, nationList.length)];
    }
}
